//
//  YZMActivityViewController.m
//  YZMicroStock
//
//  Created by apple on 17/4/28.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMActivityViewController.h"
#import <UIImageView+WebCache.h>
#import "YZClient.h"
#import "YZMActivityModel.h"
#import "YZMHomeBannerModel.h"

static NSString *const sActivityCell = @"YZMActivityCell";
@interface YZMActivityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation YZMActivityViewController

-(NSMutableArray *)datasource{
    
    if (!_datasource) {
        
        _datasource = [NSMutableArray array];
    }
    
    return _datasource;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"活动中心";
    }
    return self;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self createTableView];
    [self fetchActivityList];
}

#pragma maek createTableView
-(void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//    self.tableView.rowHeight = 110;
//    self.tableView.sectionHeaderHeight = 14;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:sActivityCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    [self createFooterView];
}

#pragma mark createfooterView
-(void)createFooterView{
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 124)];
    bgView.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.tableView.tableFooterView = bgView;
    
    UIImageView *footerView = [UIImageView new];
    [footerView setImage:[UIImage imageNamed:@"Image_wait_activity"]];
    [bgView addSubview:footerView];
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(bgView).offset(14);
        make.left.right.bottom.equalTo(bgView);
    }];
}

#pragma maek datasource delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sActivityCell forIndexPath:indexPath];
    UIImageView *imageView = [UIImageView new];
    cell.backgroundView = imageView;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    YZMActivityModel *model = _datasource[indexPath.section];
    [imageView sd_setImageWithURL:[model imageUrl]];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",indexPath);
    YZMActivityModel *model = _datasource[indexPath.section];
    NSDictionary *dic = @{@"bannerEvent":[model activityUrl], @"title":model.name, @"param":@{@"activityid":@(model.activityId)}};
    [YZMHomeBannerModel selectDirection:dic withLocal:YES];
//    [YZDHomeBannerModel selectDirection:dic withLocal:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _datasource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(void)fetchActivityList{

    WS(ws)
    [YZClient Activity_activityListsuccess:^(NSURLSessionDataTask *task, id responseObject) {
        YZGeneralResultModel *topModel =responseObject;
        if (topModel.success) {
            
            [ws.datasource removeAllObjects];
            [ws.datasource addObjectsFromArray:topModel.data];
            [ws.tableView reloadData];
        }else{
            
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
